// pages/wall_entry/wall_entry.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    entryData: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    name: "",
    holeID: 0,
    dianzanUrl: "/images/notThumb.png",
    holeThumb: 0,
    holeIfThumb: false,
    holeUserPictureUrl: "",
    height: -1,
  },


  /**
   * 组件的方法列表
   */
  lifetimes: {
    
    attached: function () {
      let entryData = this.properties.entryData
      let that = this
      let height = this.data.height

      var query = wx.createSelectorQuery().in(this)
      query.select('#outer').boundingClientRect(function (res) {
        height = res.height
        that.setData({height})
      }).exec()

      that.setData({
        holeID: entryData.holeID,
        name: entryData.holeAno ? "匿名用户" : entryData.name,
        dianzanUrl: entryData.holeIfThumb ? "/images/thumb.png" : "/images/notThumb.png",
        holeThumb: entryData.holeThumb,
        holeIfThumb: entryData.holeIfThumb ? true : false,
        holeUserPictureUrl: entryData.holeUserPictureUrl ? entryData.holeUserPictureUrl : "/images/defaultAvatar.png",
      })

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },

  },
  methods: {

    clickme: function () {
      this.setData({
        holeID: this.properties.entryData.holeID,
      })
      var data = {
        holeID: this.data.holeID
      }
      if (app.globalData.userInfo == null) {
        wx.showToast({
          title: '请先登录',
          icon: 'error',
          duration: 2000
        })
        return;
      }
      if (this.data.holeIfThumb == false) {
        //代表开始是没赞
        app.wxapi('POST', "/hole/thumbs-up", data, 'application/x-www-form-urlencoded', res => {
          console.log(res)
          if (res.success) {
            console.log("点赞成功");
            this.setData({
              holeThumb: this.data.holeThumb + 1,
              dianzanUrl: "/images/thumb.png",
              holeIfThumb: true,
            })
          } else {
            console.log("没有回应");
            wx.showModal({
              title: '提示',
              content: '提交失败',
            });
          }
        }, (err) => {
          //调用服务端登录接口失败
          console.log(err.errMsg)
        })
      } else {
        //代表开始是赞
        app.wxapi('POST', "/hole/thumbs-off", data, 'application/x-www-form-urlencoded', res => {
          //console.log(res);
          if (res.success) {
            //console.log("取消点赞成功");
            this.setData({
              holeThumb: this.data.holeThumb - 1,
              dianzanUrl: "/images/notThumb.png",
              holeIfThumb: false,
            })
          } else {
            console.log("没有回应");
            wx.showModal({
              title: '提示',
              content: '提交失败',
            });
          }
        }, (err) => {
          //调用服务端登录接口失败
          console.log(err.errMsg)
        })
      }
    },
    comment: function () {
      wx.navigateTo({
        url: '../../pages/hole_comment/hole_comment?holeID=' + JSON.stringify(this.data.holeID),
      })
    }
  }
})